//
//  FGYTabBar.m
//  yingKe
//
//  Created by ma c on 16/9/8.
//  Copyright (c) 2016年 beijing. All rights reserved.
//

#import "FGYTabBar.h"

@interface FGYTabBar ()

@property (strong, nonatomic) UIButton * lastItem;
@property (strong, nonatomic) UIButton * cameraBtn;
@property (strong, nonatomic) UIImageView * tabBgView;
@property (strong, nonatomic) NSArray * dataArray;//数据源

@end

@implementation FGYTabBar
- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[@"tab_live",@"tab_me"];
    }
    return _dataArray;
}

- (UIButton *)cameraBtn{
    if (!_cameraBtn) {
        _cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cameraBtn setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal];
        [_cameraBtn sizeToFit];
        [_cameraBtn addTarget:self action:@selector(didClickItem:) forControlEvents:UIControlEventTouchUpInside];
        _cameraBtn.tag = FGYItemTypeLaunch;
    }
    return _cameraBtn;
}

- (UIImageView *)tabBgView{
    if (!_tabBgView) {
        _tabBgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"global_tab_bg"]];
    }
    return _tabBgView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.tabBgView];
        for (int i = 0; i < self.dataArray.count; i++) {
            UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
            [item setImage:[UIImage imageNamed:self.dataArray[i]] forState:UIControlStateNormal];
            [item setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%@",self.dataArray[i],@"_p"]] forState:UIControlStateSelected];
            if (i == 0) {
                item.selected = YES;
                self.lastItem = item;
            }
            item.tag = i + FGYItemTypeLive;
            [item addTarget:self action:@selector(didClickItem:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:item];
        }
    }
    [self addSubview:self.cameraBtn];
    return self;
}

- (void)didClickItem:(UIButton *)button{
    if (self.block) {
        self.block(self,button.tag);
    }
    if (button.tag == FGYItemTypeLaunch) {
        return;
    }
    self.lastItem.selected = NO;
    button.selected = YES;
    self.lastItem = button;
    [UIView animateWithDuration:0.4 animations:^{
        button.transform = CGAffineTransformMakeScale(1.3, 1.3);
    }completion:^(BOOL finished) {
        button.transform = CGAffineTransformIdentity
        ;
    }];
    
    
}

#pragma mark -
#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat width = self.frame.size.width / self.dataArray.count;
    for (UIView * btn in self.subviews) {
        
        if (btn.tag >= FGYItemTypeLive ) {
            btn.frame = CGRectMake((btn.tag - FGYItemTypeLive) * width, 0, width, self.frame.size.height);
        }
    }
    self.tabBgView.frame = self.frame;
    self.cameraBtn.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5 - 20);
}



@end
